define(function(require,exports,module){
	require("resource/js/easyui/themes/icon.css");
	require("oa/act/approval/form/css/form.css");
	require("resource/js/easyui/easyloader.js");
	var ajax = require("resource/js/component/ajax.js");
	var ECombobox = require("resource/js/component/easycombobox.js");
	ECombobox.create({
		url:BASEPATH+"/base/authoriza/basecode/getType.htm",
		param:{checkeds:"processStatus"}
	});
	
	function initPage(){
		filedApproval(true,1,8);
		initSearchBox();
		$("#searchform").show();
	}
	var initSearchBox = function() {
		using([ "combobox" ], function() {
			ajax.init().success(function(code,msg,data){
				if(code==0){
					var arrasys = [ {
						id : '',
						name : '请选择'
					} ];
					var retData = arrasys.concat(data);
					$('#processkey').combobox({
					valueField : 'id',
					textField : 'name',
					data : retData,
					onChange : function(newValue, oldValue) {
						var starttime = $('#starttime').datebox('getValue');
						var endtime = $('#endtime').datebox('getValue');
						filedApproval(true,1,8,status,newValue,starttime,endtime);
					}
				});
				}
			}).post(BASEPATH+"/oa/act/generalOperate/getProcessList.htm",{"moudelType":"power_type_3"})
		});
	};
	
	
	//点击查询按钮
	$("#findQuery").on("click",function(){
		var processkey = $('#processkey').combobox('getValue');
		var starttime = $('#starttime').datebox('getValue');
		var endtime = $('#endtime').datebox('getValue');
		filedApproval(true,1,8,status,processkey,starttime,endtime);
	});
	
	//查看详情
	var openDetails = function(row) {
		 $(".jgui-drawer-right").removeClass("jgui-drawer-close").addClass("jgui-drawer-open");
		 $(".duty_drawer").show();
		 $("#jgui-title").text('抄送我的（'+row.processname+'）');
		if(row.status==0){
			$("#archiveFile").hide();
			$("#cancelFile").show();
			$("#remindersFile").show();
		}else{
			$("#archiveFile").show();
			$("#cancelFile").hide();
			$("#remindersFile").hide();
		}
		var urlPath = "";
		if (row.status == '0' || row.status == '2') {
			urlPath = BASEPATH + "/oa/act/activitiMain/getProcessViewPage.htm?key=" + row.processkey + "&bussId=" + row["businessid"];
		} else {
			urlPath = BASEPATH + "/view/oa/act/main/actProcessHistory?url=" + row["path"] + "&bussId=" + row["businessid"];
		}
		$("#myiframe").attr("src",urlPath);
		
		//打印
		$("#printFile").unbind("click");
		$("#printFile").on("click",function(){
			window.parent.$(".oa-maskAll").show();
			ajax.init().success(function(code,msg,data){
				window.parent.$(".oa-maskAll").hide();
				var param=data.filePath+data.fileId+"."+data.fileExt;
				if(code==0){
					$.messager.confirm("系统提示",msg,function(r){
						if(r){
							window.open(BASEPATH+"/download/file/"+param);
						}
					});
					
				}
			}).error(function(code,msg,data){
				window.parent.$(".oa-maskAll").hide();
				if(msg!="")
					$.messager.show('系统提示',msg);
			}).post(BASEPATH+"/oa/act/print/printprocess.htm",{"businessId":row.businessid})	
		});
	  }
	
	var colorNumber = {
			0 : "orange",
			1 : "green",
			2 : "purple",
			3 : "red",
			4 : "grey"
	}
	function parseAbstract(abstractStr){
		var separator = "@VITO@";
		if(abstractStr){
			return abstractStr.split(separator);
		}
	}
	//删除数据列表加载
	var filedApproval = function(flag,pageNo,pageSize,status,processkey,starttime,endtime){
		if(flag){
			$("#dataList ul").text("");
		 }
		var option = {
				"pageNo":pageNo,
				"pageSize":pageSize,
				"processkey":processkey||"",
				"starttime":starttime||"",
				"endtime":endtime||""
		    }
		ajax.init().success(function(code,msg,data){
			if(data.rows.length>0){
				$("#noData").hide();
				for(var k in data.rows){
					var abstractStr = data.rows[k]['abstract'] || '';
					if(!data.rows[k].processname){
						data.rows[k].processname="流程已被删除"
					}
					$("<li><div><span class="+colorNumber[data.rows[k].status]+">"+data.rows[k].statustext+"</span></div>" +
					      "<div>"+workBeanchTrim(data.rows[k].processname)+"</div>" +
					      "<div><xml>"+contentNotempty(parseAbstract(abstractStr)&&parseAbstract(abstractStr)[0])+"</xmp></div>" +
					      "<div>发起人："+workBeanchTrim(data.rows[k].startuser)+"</div>" +
					      "<div>"+showEmptyEnd(data.rows[k].assigneename,data.rows[k].starttime)+"</div>" +
					      "<div>"+showEmptyEnd(null,data.rows[k].starttime,data.rows[k].endtime)+"</div></li>").appendTo($("#dataList ul")).each(function(){
								$.data(this,{data:data.rows[k]})
							}).on("click",function(){
						       $(this).addClass("selected").siblings().removeClass("selected");
					    	   openDetails($.data(this).data);
					       });
				}
				$("#dataList").find(".query-more").remove();
				if(data.rows&&data.rows.length>=pageSize && data.total>8){
					$("<div class='query-more'><a>查看更多</a></div>").appendTo($("#dataList")).on("click",function(){
						pageNo++;
						filedApproval(false,pageNo,pageSize,status,processkey,starttime,endtime);
					});
				}else{
					if(data.total>8){
						$("<div class='query-more'><a style='cursor: default;'>没有更多数据了。</a></div>").appendTo($("#dataList"));
					}
				}
			}else{
				$("#noData").show();
			}
			$(".jgui-drawer-right").find(".close").click(function(){
				$(".jgui-drawer-right").addClass("jgui-drawer-close").removeClass("jgui-drawer-open");
			});
		}).post(BASEPATH + "/oa/actCC/queryCCtoMePage.htm",option)
	}
	
	//去除后端传undefined值
	var workBeanchTrim = function(res){
		return res || "";
	}
	
	//内容不能为空，超过30字符显示......
	var contentNotempty =function(content){
		if(content){
			if(content.toString().length>30){
				return content.toString().substring(0,30)+'......';
			}else if(content.toString().length<=30){
				return content.toString().substring(0,30);
			}
		}else{
			return "";
		}
	}
	var showEmptyEnd = function(content,startTime,endTime){
		if(content){
			return "下级审批人：" + content;
		}else if(startTime){
			if(endTime){
				return "结束时间："+ endTime;
			}else{
				return "发起时间："+ startTime;
			}
		}
	}
	module.exports = {
		trans : function() {
			using(["parser", "textbox", "numberbox", "dialog",
					"combobox", "messager" ], function() {
				initPage();
			});
		}
	}
	
});

